package bdz.projects.cbt.core
{
	import bdz.common.model.AppEnvironment;
	
	import flash.events.EventDispatcher;

	public class CBTPlayer extends EventDispatcher
	{
		private var _playerName:String;
		private var _realName:String;
		private var _email:String;
		
		private var _feedbackPoint:Number = 0;
		private var _inventory:CBTInventory;
		private var _cash:CBTCash;
		private var _paypal:CBTCash;
		private var _mortgage:CBTCash;
		
		private var _soldItems:Array;
		private var _env:AppEnvironment = AppEnvironment.getEnv();
		private var _maxDays:Number;
		public function CBTPlayer()	{
			_soldItems = new Array();
			_mortgage = new CBTCash();
			_paypal = new CBTCash();
			_cash = new CBTCash();
			_inventory = new CBTInventory(_env.getSetting("startCapacity") || 100);
			super();
		}
		public function get mortgage():CBTCash{
			return _mortgage;
		}
		public function get paypal():CBTCash{
			return _paypal;
		}
		public function get cash():CBTCash{
			return _cash;
		}
		public function set startingCash(n:Number):void{
			_cash.addAmount(n);
		}
		public function set startingMortgage(n:Number):void{
			_mortgage.addAmount(n);
		}
		public function set startingPaypal(n:Number):void{
			_paypal.addAmount(n);
		}
		public function get cashFormatted():String{
			return _cash.formattedValue;
		}
		public function get paypalFormatted():String{
			return _paypal.formattedValue;
		}
		public function get mortgageFormatted():String{
			return _mortgage.formattedValue;
		}
		public function get feedback():Number{
			return _feedbackPoint;
		}
		public function set playerName(s:String):void{
			_playerName = s;	
		}
		public function set realName(s:String):void{
			_realName = s;	
		}
		public function set email(s:String):void{
			_email = s;
		}
		public function get playerName():String{
			return _playerName;
		}
		public function get realName():String{
			return _realName;
		}
		public function get email():String{
			return _email;
		}
		public function get inventory():CBTInventory{
			return _inventory;
		}
		public function addFeedback(n:Number):void{
			_feedbackPoint += n;	
		}
		
		public function get totalWorth():Number{
			return _inventory.totalValue+_cash.value+_paypal.value;
		}
		public function addSoldItem(nme:String, count:Number):void{
			for each(var obj:Object in _soldItems){
				if(obj.name == nme){
					obj.count++;
					return;
				}
			}
			_soldItems.push({name:nme, count:count});
		}
		public function get mostSold():String{
			try{
				var cpy:Array = _soldItems.slice();
				cpy.sortOn("count", Array.NUMERIC | Array.DESCENDING);
				return cpy[0].name;
			}catch(e:Error){}
			return "blahblah";
		}
		public function set maxDays(n:Number):void{
			_maxDays = n; 
		}
		public function get maxDays():Number{
			return _maxDays;
		}
		
		
	}
}